<?php
/**
 * ===============================================
 * 版权所有 2019-2050 青岛铁锤科技有限公司，并保留所有权利。
 * 网站地址: http://www.huaxianling.com
 * ===============================================
 * Author: Happy dong
 * Created Time: 2019-07-17 13:58
 *
 * Version: 2.0
 */

namespace App\Modules\Employee\Controllers;

use App\Lib\Facades\FLAuth;
use App\Lib\Utilities;
use App\Modules\Employee\Services\PortfolioService;
use App\Modules\Employee\Transformers\AppointmentBgTransformer;
use App\Modules\Employee\Transformers\getNewLikePortfolioTransformer;
use App\Modules\Employee\Transformers\GetPortfolioListTransformer;
use App\Modules\Employee\Validators\PortfolioControllerValidator;
use Flower\Core\Http\BaseController;
use Flower\Core\Http\Request;

class PortfolioController extends BaseController
{
	private $portfolioService;
	public function __construct(PortfolioService $portfolioService)
	{
		$this->portfolioService = $portfolioService;
	}

	public function getRules()
	{
		return PortfolioControllerValidator::validator();
	}


	/**
	 * 新增作品集
	 * @param Request $request
	 * @return mixed
	 * @throws \App\Exceptions\NotFoundException
	 */
	public function addPortFolio(Request $request)
	{
		$res = $this->portfolioService->addPortFolio(FLAuth::id(),$request->name,$request->service_id,$request->resource_id,$request->desc,$request->store_id);
		if (empty($res)) return $this->error("新增失败！");
		return $this->success("新增成功！");
	}

	/**
	 * 编辑作品集
	 * @param Request $request
	 * @return mixed
	 * @throws \App\Exceptions\NotFoundException
	 */
	public function editPortfolio(Request $request){

		$res = $this->portfolioService->addPortFolio(FLAuth::id(),$request->name,$request->service_id,$request->resource_id,$request->desc,$request->store_id,$request->portfolio_id);
		if (empty($res)) return $this->error("编辑失败！");
		return $this->success("编辑成功！");
	}

	/**
	 * 微信端的 首页 店铺 理发师中心 获取作品集
	 * @param Request $request
	 * @return mixed
	 * @throws \Flower\Core\Repository\Exceptions\RepositoryException
	 */
	public function getPortfolioList(Request $request)
	{
		$list = $this->portfolioService->getPortFolioList($request->store_id,$request->barber_id,$request->page_size,$request->keyword,FLAuth::id());
		$list = (new GetPortfolioListTransformer($list,false,false,FLAuth::id()))->transform();
		$list = Utilities::dealPage($list);
		return $this->display($list);
	}


    public function getPortfolio(Request $request)
    {
        $portfolio_id = $request->input('portfolio_id');
        $list = $this->portfolioService->getPortFolioList($request->store_id,$request->barber_id,$request->page_size,$request->keyword,FLAuth::id(),$portfolio_id);
        if (empty($list)) return $this->error("查询失败！");
        $list = (new GetPortfolioListTransformer($list))->transform();
        $list = Utilities::dealPage($list);
        return $this->display($list);
    }

	/**
	 * 作品集列表
	 * @param Request $request
	 * @return mixed
	 * @throws \Flower\Core\Repository\Exceptions\RepositoryException
	 */
	public function getPortfolioListApp(Request $request)
	{
		$res = $this->portfolioService->getPortFolioList($request->store_id,FLAuth::id(),$request->page_size);
		if (empty($res)) return $this->error("查询失败！");
        $list = (new GetPortfolioListTransformer($res,false,true))->transform();
        $list = Utilities::dealPage($list);
		return $this->display($list);
	}


	/**
	 * 删除作品集
	 * @param Request $request
	 * @return mixed
	 * @throws \App\Exceptions\NotFoundException
	 */
	public function delPortfolio(Request $request)
	{
		$res = $this->portfolioService->delPortfolio(FLAuth::id(),$request->portfolio_id);
		if (empty($res)) return $this->error("删除失败！");
		return $this->success("删除成功");
	}


	/**
	 * 点赞
	 * @param Request $request
	 * @return mixed
	 */
	public function likePortfolio(Request $request){
		$res = $this->portfolioService->likePortfolio(FLAuth::id(),$request->portfolio_id);
		if (empty($res)) return $this->error("点赞失败！");
		return $this->success("点赞成功");
	}

	/**
	 * 取消点赞
	 * @param Request $request
	 * @return mixed
	 */
	public function unlikePortfolio(Request $request){
		$res = $this->portfolioService->delLike(FLAuth::id(),$request->portfolio_id);
		if($res){
            return $this->success("取消点赞成功");
        } else{
            return $this->error("取消点赞失败");
        }
	}

	/**
	 * 作品集播放次数加1
	 */
	public function setIncPortfolio(Request $request){
		$res = $this->portfolioService->setInc($request->all());
		if (empty($res)) return $this->error("播放次数加1失败！");
		return $this->success("播放次数加1成功");
	}

	/**
	 * 作品集预约bg
	 * @param Request $request
	 * @return mixed
	 */
	public function getAppointmentBg(Request $request){
		$res =  $this->portfolioService->getAppointmentBg($request->portfolio_id);
		return $this->display((new AppointmentBgTransformer($res))->transform(),"信息获取成功！");
	}

	/**
	 * 用户是否关注理发师和是否点赞作品集
	 * @param Request $request
	 * @return mixed
	 */
	public function isLikeAndFollow(Request $request){
		$isLikeAndFollow = $this->portfolioService->isLikeAndFollow(FLAuth::id(),$request->portfolio_id,$request->barber_id);
		return $this->display($isLikeAndFollow);
	}


	/**
	 *  最新关注的理发师作品集
	 * @param Request $request
	 * @return mixed
	 */
	public function getNewLikePortfolio(Request $request){
		$list = $this->portfolioService->getNewLikePortfolio(FLAuth::id(),$request->page_size);
		$list = (new getNewLikePortfolioTransformer($list))->transform();
		return $this->display($list,"信息获取成功");
	}
	/**
	 * 新增评论
	 * @param Request $request
	 * @return mixed
	 */
//	public function addReplyPortfolio(Request $request){
//		$res = $this->portfolioService->addReply($request->all());
//		if(empty($res)) return $this->error("评论新增失败");
//		return $this->success("评论新增成功");
//	}

	/**
	 * 评论删除
	 * @param Request $request
	 * @return mixed
	 */
//	public function delReplyPortfolio(Request $request){
//		$res = $this->portfolioService->delReply($request->all());
//		if(empty($res)) return $this->error("评论删除失败");
//		return $this->success("评论删除成功");
//	}
}
